﻿Imports VendasBLL
Imports VendasDAL
Public Class CarroFRM
    Public Tipo As String
    Dim ObjBLL As New CarroBLL
    Public Op As String
    Public codigo, CodMarca As Integer
#Region "Carrega Campos"
    Sub CarregaCampos(codigo As Integer)
        ObjBLL = New CarroBLL(codigo)
        LBCodigo.Text = "Código: " & ObjBLL.Codigo
        LBTitulo.Text = ObjBLL.Descricao
        TXTDescricao.Text = ObjBLL.Descricao
        CBStatus.SelectedValue = ObjBLL.Status
        TXTModelo.Text = ObjBLL.Modelo
        TXTMarca.Text = ObjBLL.MarcaDescricao

    End Sub
#End Region

#Region "Limpar campos"
    Sub Limpa()
        CBStatus.SelectedValue = 1
        TXTDescricao.Text = ""
        TXTMarca.Text = ""
        TXTModelo.Text = ""
        Me.ObjBLL = Nothing
    End Sub
#End Region

#Region "Preenche campos"
    Sub PreencheCampos()
        CBStatus.SelectedValue = ObjBLL.Status
        TXTDescricao.Text = ObjBLL.Descricao
    End Sub

#End Region

#Region "Load"
    Private Sub CarroFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo < 3", "sta_codigo")
        Limpa()
        If (Op = "I") Then
            LBCodigo.Visible = False
            LBTitulo.Text = "Carro"
            LBCodigo.Text = 0
        Else
            LBCodigo.Visible = True
            CarregaCampos(codigo)
        End If
    End Sub
#End Region

#Region "Salvar/Alterar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click

        Try

            ObjBLL = New CarroBLL(codigo, StrConv(Trim(TXTDescricao.Text), VbStrConv.ProperCase), CBStatus.SelectedValue, StrConv(Trim(TXTModelo.Text), VbStrConv.ProperCase), CodMarca)
            If Op = "I" Then
                ObjBLL.Inserir()
                MessageBox.Show("Carro inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            Else
                ObjBLL.Alterar()
                MessageBox.Show("Carro alterado com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Tipo = "ret"
        ConsultaMarcaFRM.ShowDialog()
    End Sub


    Private Sub CarroFRM_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
End Class